package com.eliza.demo.db.activity

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.eliza.demo.R
import com.eliza.library.model.Contactor
import com.eliza.library.tools.TextTools


/*-*- coding:utf-8 -*-
 * @Author  : debi
 * @Time    : 5/1/22
 * @Software: Android Studio
 */
class ContactorAdapter : RecyclerView.Adapter<ContactorAdapter.ContactorHolder> {
    lateinit var conList: List<Contactor>

    constructor(conList: List<Contactor>) : super() {
        this.conList = conList
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): ContactorAdapter.ContactorHolder {
        val root: View =
            LayoutInflater.from(parent.context)
                .inflate(
                    R.layout.activity_contactor_item,
                    parent,
                    false
                )
        return ContactorHolder(root)
    }

    override fun onBindViewHolder(holder: ContactorHolder, position: Int) {
        val contactor: Contactor = conList.get(position)
        holder.cId.text = contactor.id.toString()
        holder.cName.text = contactor.name
        holder.cAge.text = TextTools.StringToInt(contactor.age.toString()).toString()
        holder.cTel.text = contactor.tel
        holder.cEml.text = contactor.email
    }

    override fun getItemCount(): Int = conList.size

    class ContactorHolder(val itemView: View) :
        RecyclerView.ViewHolder(itemView) {
        var cId = itemView.findViewById<TextView>(R.id.Contactor_item_id)
        var cName = itemView.findViewById<TextView>(R.id.Contactor_item_name)
        var cAge = itemView.findViewById<TextView>(R.id.Contactor_item_age)
        var cTel = itemView.findViewById<TextView>(R.id.Contactor_item_tel)
        var cEml = itemView.findViewById<TextView>(R.id.Contactor_item_email)
    }

}